package com.compass.mobile.ui.dialog

import AppManager
import android.annotation.SuppressLint
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import androidx.activity.viewModels
import androidx.fragment.app.FragmentActivity
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.BaseGridView
import androidx.leanback.widget.FocusHighlight
import androidx.leanback.widget.FocusHighlightHelper
import com.compass.mobile.callback.OnItemClickListener
import com.compass.mobile.callback.OnPatientItemClickListener
import com.compass.mobile.databinding.DialogChoosePatientBinding
import com.compass.mobile.framework.base.BaseDialog
import com.compass.mobile.framework.base.BaseDialogFragment
import com.compass.mobile.framework.constant.YD_USER_ID
import com.compass.mobile.framework.constant.selfServicePhone
import com.compass.mobile.framework.constant.selfServiceWxCode
import com.compass.mobile.framework.utils.dpToPx
import com.compass.mobile.models.PatientBean
import com.compass.mobile.presenters.PatientChoosePresenter
import com.compass.mobile.widget.MyBridgeAdapter

/**
 * Description： 添加就诊人提示
 * Date:2024/6/19 16:30
 */
class ChoosePatientDialog {
    class Builder(
        activity: FragmentActivity,
        private var onBottomClick: OnPatientItemClickListener,
        private var patientList: MutableList<PatientBean>
    ) : BaseDialogFragment.Builder<Builder>(activity) {

        private val mBinding: DialogChoosePatientBinding =
            DialogChoosePatientBinding.inflate(LayoutInflater.from(activity))

        init {
            setContentView(mBinding.root)
            setWidth((AppManager.getScreenHeightPx() * 0.6).toInt())
//            setHeight((AppManager.getScreenHeightPx()*0.8).toInt())
            setAnimStyle(BaseDialog.AnimStyle.DEFAULT)
            setGravity(Gravity.CENTER)
            initView()
        }

//        private val userViewModel: UserViewModel by this@Builder.context.asActivity.viewModels()

        private lateinit var arrayObjectAdapter: ArrayObjectAdapter
//        private var patientList: MutableList<PatientBean> = mutableListOf()//包装集合

        private fun initView() {
            specialColumn()
            mBinding.gvPatients.requestFocus()
//            userViewModel.gePatients()

//            userViewModel.patientLiveData.observe(activity){list->
//                if(list!=null){
//                    patientList = list
////                    patientList.add(PatientBean("10086"))
//                    if (patientList.isNotEmpty()) {
////                        mBinding.emptyView.gone()
//                        arrayObjectAdapter.clear()
//                        arrayObjectAdapter.addAll(0, patientList)
//                        mBinding.gvPatients.requestFocus()
//                    } else {
////                        mBinding.emptyView.visible()
//                    }
//                }
//            }
//            mBinding.gvPatients  =
//            mBinding.tvLocalNumber.text= phoneNumber
//            mBinding.llLocalNumber.onClick {
//                onBottomClick.onButtonCallBack(1,"local")
//                dismiss()
//            }
//            mBinding.llOtherNumber.onClick {
//                onBottomClick.onButtonCallBack(2,"other")
//                dismiss()
//            }
//            mBinding.llScanCode.onClick {
//                onBottomClick.onButtonCallBack(3,"code")
//                dismiss()
//            }
        }

        /**
         *  患者item点击事件
         */
//        private val patientItemClickListener : OnItemClickListener  = object:OnItemClickListener{
//            override fun onItemClick(position: Int, view: View) {
//                val patientBean  = patientList[position]
//                onBottomClick.onItemClick(patientBean,view)
//            }
//        }

        private val patientItemClickListener: OnItemClickListener = object : OnItemClickListener {
            override fun onItemClick(position: Int, view: View) {
                val patientBean  = patientList[position]
                onBottomClick.onItemClick(patientBean,view)
            }
        }

        @SuppressLint("RestrictedApi")
        fun specialColumn() {
            mBinding.gvPatients.verticalSpacing = dpToPx(10)
            mBinding.gvPatients.setNumColumns(1)
            arrayObjectAdapter = ArrayObjectAdapter(PatientChoosePresenter())
            val itemBridgeAdapter = MyBridgeAdapter(
                context,
                arrayObjectAdapter,
                //接口定义为成员变量时无法调用回调 很奇怪
                object : OnItemClickListener {
                    override fun onItemClick(position: Int, view: View) {
                        dismiss()
                        val patientBean  = patientList[position]
                        onBottomClick.onItemClick(patientBean,view)
                    }
                }
//                patientItemClickListener
            )
            mBinding.gvPatients.setAdapter(itemBridgeAdapter)
            mBinding.gvPatients.setFocusScrollStrategy(BaseGridView.FOCUS_SCROLL_ITEM)
            arrayObjectAdapter.addAll(0, patientList)
            FocusHighlightHelper.setupBrowseItemFocusHighlight(
                itemBridgeAdapter,
                FocusHighlight.ZOOM_FACTOR_NONE, false
            )
            mBinding.gvPatients.requestFocus()
        }

    }
}